package com.sxf.biz.job;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sxf.auth.enums.ProjectStatusEnum;
import com.sxf.biz.customer.dao.ProjectMapper;
import com.sxf.biz.model.Project;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

/**
 * @description: TODO
 * @Author Leo
 * @date 2024/4/520:32
 */
@Component
public class SchedulerJobService {
    private static final Logger logger = LoggerFactory.getLogger(SchedulerJobService.class);
    @Autowired
    ProjectMapper projectMapper;

    /**
     * 校验项目是否开始
     * 每天凌晨零点5分执行
     */
    @Scheduled(cron = "0 5 0 * * ? ")
//    @Scheduled(cron = "0 0/5 * * * ? ")
    public void scanProjectStatus(){
        logger.error("项目开始日期校验任务开始—>>>>>");
        QueryWrapper<Project> queryWrapper = new QueryWrapper<Project>();
        queryWrapper.lambda().eq(Project::getStatus,ProjectStatusEnum.UN_START.getCode());
        List<Project> projects = projectMapper.selectList(queryWrapper);
        Date now = DateUtil.beginOfDay(new Date());
        if(projects.size() > 0){
            logger.error("未开始项目数量---{}",projects.size());
            for(Project prj : projects){
                if(!prj.getStartDate().after(now)){
                    prj.setStatus(ProjectStatusEnum.IN_PROGRESS.getCode());
                    projectMapper.updateById(prj);
                }
            }
        }
        logger.error("项目开始日期校验任务结束—<<<<<");
    }
}
